package com.rlyy.basic.process;


import com.rlyy.common.param.Request;
import com.rlyy.common.response.Result;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * 调度处理器
 *
 * @author yuanqinglong
 * @since 2021/9/15 9:09
 */
public abstract class AbstractProcessorDispatcher {

    /**
     * 异步
     */
    private volatile boolean async = false;

    private final Map<Boolean, ISyncAsyncProcessor> syncAsyncProcessorMap;

    AbstractProcessorDispatcher(List<ISyncAsyncProcessor> syncAsyncProcessors) {
        this.syncAsyncProcessorMap = syncAsyncProcessors.stream().collect(Collectors.toMap(ISyncAsyncProcessor::asyncProcess, Function.identity()));
    }


    /**
     * 执行过程
     *
     * @param request 完整的请求
     * @return {@link Result}
     * @throws Exception 异常
     */
    public abstract Result process(Request request) throws Exception;


    public synchronized void setAsync(boolean async) {
        this.async = async;
    }

    public ISyncAsyncProcessor getSyncAsyncProcessor() {
        return syncAsyncProcessorMap.get(async);
    }
}
